package com.bjsxt.shop.goods.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.shop.common.util.ConstatFinalUtil;
import com.bjsxt.shop.common.util.PageInfoUtil;
import com.bjsxt.shop.goods.dao.IAGoodsBrandDao;
import com.bjsxt.shop.goods.dao.IAGoodsCateDao;
import com.bjsxt.shop.goods.dao.IAGoodsDao;
import com.bjsxt.shop.goods.pojo.AGoods;
import com.bjsxt.shop.goods.pojo.AGoodsBrand;
import com.bjsxt.shop.goods.pojo.AGoodsCate;
import com.bjsxt.shop.goods.service.IGoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("goodsService")
public class GoodsServiceImpl implements IGoodsService
{
	@Resource
	private IAGoodsBrandDao goodsBrandDao;
	@Resource
	private IAGoodsCateDao goodsCateDao;
	@Resource
	private IAGoodsDao goodsDao;
	
	//保存一个品牌
	@Override
	public JSONObject saveOneGoodsBrandService(AGoodsBrand goodsBrand)
	{
		JSONObject resultJSON = new JSONObject();
		//影响条数
		int res = this.goodsBrandDao.save(goodsBrand);
		//判断
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			//存储data
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsBrand.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;		
	}
	
	// 保存一条品牌,goodsBrand:保存的对象,paramsMap:上传文件所需要的条件	
	@Override
	public JSONObject saveOneGoodsBrandService(AGoodsBrand goodsBrand,Map<String, Object> paramsMap)
	{
		/* 保存品牌 */
		JSONObject resultJSON = this.saveOneGoodsBrandService(goodsBrand);
		return resultJSON;
	}
	
	//更新一个品牌
	@Override
	public JSONObject updateOneGoodsBrandService(AGoodsBrand goodsBrand)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsBrandDao.update(goodsBrand);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsBrand.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}
	
	//更新一个品牌,goodsBrand:保存的对象,paramsMap:上传文件所需要的条件	
	@Override
	public JSONObject updateOneGoodsBrandService(AGoodsBrand goodsBrand,Map<String, Object> paramsMap)
	{
		JSONObject resultJSON = this.updateOneGoodsBrandService(goodsBrand);
		return resultJSON;
	}

	//删除一个品牌
	@Override
	public JSONObject deleteOneGoodsBrandService(AGoodsBrand goodsBrand)
	{
		JSONObject resultJSON = new JSONObject();
		return resultJSON;
	}
	
	//查询一条品牌,condMap:查询条件,return 返回品牌
	@Override
	public AGoodsBrand findOneGoodsBrandService(Map<String, Object> condMap)
	{
		return this.goodsBrandDao.findOne(condMap);
	}
	
	//查询多条品牌,pageInfoUtil:分页对象:如果为null,不分页,如果不为空,分页,condMap	查询条件,return 返回多条记录
	@Override
	public Map<String,Object> findCondListGoodsBrandService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//为关键字加上%,实现模糊查询
		if(condMap.get("keyword") !=null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		//判断
		if(pageInfoUtil != null)
		{
			//分页
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			//查询总记录数
			List<AGoodsBrand> roleList = this.goodsBrandDao.findList(condMap);
			//使用dubbo的时候，不能为参数新增加赋值
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
			resultMap.put("list", roleList);
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			return resultMap;
		}
		//不分页查询
		List<AGoodsBrand> roleList = this.goodsBrandDao.findList(condMap);
		resultMap.put("list", roleList);		
		return resultMap;		
	}
	
	@Override
	public AGoodsCate findOneGoodsCateService(Map<String, Object> condMap)
	{
		return this.goodsCateDao.findOne(condMap);
	}
	//保存一条商品分类
	@Override
	public JSONObject saveOneGoodsCateService(AGoodsCate goodsCate)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsCateDao.save(goodsCate);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsCate.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	//修改一条商品分类
	@Override
	public JSONObject updateOneGoodsCateService(AGoodsCate goodsCate)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsCateDao.update(goodsCate);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsCate.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}
	//删除一条商品分类
	@Override
	public JSONObject deleteOneGoodsCateService(AGoodsCate goodsCate)
	{
		JSONObject resultJSON = new JSONObject();
		return resultJSON;
	}
	
	//查询多条商品分类
	@Override
	public Map<String, Object> findCondListGoodsCateService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		Map<String, Object> resultMap = new HashMap();
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AGoodsCate> roleList = this.goodsCateDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			
			resultMap.put("list", roleList);
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			return resultMap;
		}
		/* 不分页查询数据 */
		List list = this.goodsCateDao.findList(condMap);
		resultMap.put("list", list);
		return resultMap;
	}
	//查询商品分类-树形菜单
	@Override
	public List<AGoodsCate> findTreeListCateService()
	{
		//存出结果
		List<AGoodsCate> resultList = new ArrayList<AGoodsCate>();
		//先查询所有一级节点
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("parentId", "0");
		Map<String,Object> resultMap = this.findCondListGoodsCateService(null, condMap);
		List<AGoodsCate> cateList = (List<AGoodsCate>) resultMap.get("list");
		//循环
		for (Iterator iterator = cateList.iterator(); iterator.hasNext();)
		{
			AGoodsCate cateTemp = (AGoodsCate) iterator.next();
			this.findTreeListCateServiceUtil(cateTemp, 0, resultList);
		}
		return resultList;
	}
	
	//列出树形结构的递归,
	private void findTreeListCateServiceUtil(AGoodsCate cate,int level,List<AGoodsCate> goodsCateList)
	{
		//增加前置字符串
		StringBuffer preStrsb = new StringBuffer();
		for (int i = 0; i < level; i++)
		{
			preStrsb.append("|-->");
		}
		
		//列出树形结构
		cate.setTreeName(preStrsb.toString() + cate.getName());
		//放到容器中
		goodsCateList.add(cate);
		
		//查询子节点
		Map<String, Object> condMap = new HashMap();
		condMap.put("parentId", cate.getId());
		Map<String, Object> resultMap = this.findCondListGoodsCateService(null, condMap);
		List<AGoodsCate> childList = (List<AGoodsCate>) resultMap.get("list");
		//判断子节点
		if(childList.size() > 0)
		{
			//非叶子节点
			cate.setLeafStatus("0");
			//判断
			for (Iterator iterator = childList.iterator(); iterator.hasNext();)
			{
				AGoodsCate cateTemp = (AGoodsCate) iterator.next();
				this.findTreeListCateServiceUtil(cateTemp, level + 1, goodsCateList);
			}
		}else
		{
			//叶子节点
			cate.setLeafStatus("1");
		}

	}
	//查询提条商品记录
	@Override
	public AGoods findOneGoodsService(Map<String, Object> condMap)
	{
		return this.goodsDao.findOne(condMap);
	}
	//保存一条商品记录
	@Override
	public JSONObject saveOneGoodsService(AGoods goods)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsDao.save(goods);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goods.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	//保存一条商品,goodsBrand:保存的对象,paramsMap:上传文件所需要的条件	
	public JSONObject saveOneGoodsService(AGoods goods,Map<String, Object> paramsMap)
	{
		JSONObject resultJSON = this.saveOneGoodsService(goods);
		return resultJSON ; 
	}
	//修改一条商品记录
	@Override
	public JSONObject updateOneGoodsService(AGoods goods)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsDao.update(goods);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goods.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}
	//删除一条商品记录
	@Override
	public JSONObject deleteOneGoodsService(AGoods goods)
	{
		JSONObject resultJSON = new JSONObject();
		return resultJSON;
	}
	//查询多条商品,pageInfoUtil:分页对象:如果为null,不分页,如果不为空,分页,condMap	查询条件,return 返回多条记录
	@Override
	public Map<String, Object> findCondListGoodsService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		//设置一个空集合
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//模糊查询
		if(condMap.get("keyword") !=null && !"".equals(condMap.get("keyword")))
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%" );
		}
		//判断
		if(pageInfoUtil != null)
		{
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AGoods> goodsList = this.goodsDao.findList(condMap);
			resultMap.put("list", goodsList);
			//因为dubbo
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());;
			//设置总记录数
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			return resultMap;
		}
		//不分页查询
		List<AGoods> goodsList = this.goodsDao.findList(condMap);
		resultMap.put("list", goodsList);
		return resultMap;		
	}
	
}
